
import Middleware from '../lib/SimpleMiddleware'

import CommonService from '../Service/CommonService'

type RequestEvent =
  'eat' | 'fly' | 'both'

interface IInAppRequest {
  type: RequestEvent
  // 表示有其他任意类型.
  // 键为字符串，值定为any 的字段
  [key:string]: any
}

export default function (request:IInAppRequest, sender:any, sendResponse:Function) {
  switch (request.type) {
    case "eat":
      setTimeout(() => {
        sendResponse({ success: true })
      }, 800)
      return true
    case "fly":
      sendResponse({ success: true, type: "fly" })
    case "both":
      Middleware.regist(
        'both',
        [CommonService.firstService, CommonService.secondService],
        request, sender, sendResponse
      )
      return true
    default:
      sendResponse({ success: false, code: 404 })
      break;
  }
}